package com.vr.xuecheng.content.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.content.mapper.CoursePublishMapper;
import com.vr.xuecheng.content.model.entity.CourseBase;
import com.vr.xuecheng.content.model.entity.CoursePublish;
import com.vr.xuecheng.content.model.entity.CoursePublishPre;
import com.vr.xuecheng.content.model.vo.CourseBaseOfPreviewVO;
import com.vr.xuecheng.content.model.vo.CoursePreviewVO;
import com.vr.xuecheng.content.service.ICourseBaseService;
import com.vr.xuecheng.content.service.ICoursePublishPreService;
import com.vr.xuecheng.content.service.ICoursePublishService;
import com.vr.xuecheng.content.service.ITeachPlanService;
import com.vr.xuecheng.messagesdk.model.entity.MqMessage;
import com.vr.xuecheng.messagesdk.service.MqMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程发布 服务实现类
 * </p>
 *
 * @author hsh
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CoursePublishServiceImpl extends ServiceImpl<CoursePublishMapper, CoursePublish> implements ICoursePublishService {
    private final ICourseBaseService courseBaseService;
    private final ITeachPlanService teachPlanService;
    private final ICoursePublishPreService coursePublishPreService;
    private final CoursePublishMapper coursePublishMapper;
    private final MqMessageService mqMessageService;
    @Override
    public CoursePreviewVO getCoursePreviewInfo(Long courseId) {
        // 获取课程信息和课程营销信息

        CourseBaseOfPreviewVO courseBaseOfPreviewVO = courseBaseService.getCourseBaseInfoById(courseId);
        List<Tree<Long>> treeNodes = teachPlanService.getTreeNodesById(courseId);
        return CoursePreviewVO.builder()
                .courseBase(courseBaseOfPreviewVO)
                .teachplans(treeNodes).build();
    }

    @Override
    @Transactional
    public boolean publish(Long courseId) {
        // 从预发布表中获取要发布的课程
        CoursePublishPre coursePublishPre = coursePublishPreService.getById(courseId);
        if (coursePublishPre == null) {
            XueChengPlusException.cast("发布失败，课程未提交");
        }
        CoursePublish coursePublish = BeanUtil.toBean(coursePublishPre, CoursePublish.class)
                // 更新为发布状态
                .setStatus("203002")
                .setCreateDate(coursePublishPre.getCreateDate())
                .setOnlineDate(coursePublishPre.getCreateDate());
        CoursePublish selectResult = coursePublishMapper.selectById(courseId);
        if (selectResult==null) {
            coursePublishMapper.insert(coursePublish);
        } else {
            coursePublishMapper.updateById(coursePublish);
        }
        courseBaseService.lambdaUpdate().eq(CourseBase::getId, courseId)
                .set(CourseBase::getStatus, "203002")
                .update();
        MqMessage mqMessage = mqMessageService.lambdaQuery()
                .eq(MqMessage::getBusinessKey1, String.valueOf(courseId))
                .one();
        if (mqMessage == null) {
            // 保存到消息中
            mqMessageService.addMessage("course_publish", String.valueOf(courseId), null, null);
        }
        // 删除预发布信息
        coursePublishPreService.removeById(courseId);
        return true;
    }

    @Override
    public boolean courseOffline(Long courseId) {
        if (courseId == null) {
            XueChengPlusException.cast("参数不能为空，下架失败");
        }
        CoursePublish coursePublish = coursePublishMapper.selectById(courseId);
        if (coursePublish==null) {
            XueChengPlusException.cast("下架的课程未上架，下架失败");
        }
        // 更新course_publish状态为下线
        int row1 = coursePublishMapper.update(null, new UpdateWrapper<CoursePublish>()
                .lambda()
                .eq(CoursePublish::getId, courseId)
                .set(CoursePublish::getStatus, "203003")
                .set(CoursePublish::getOfflineDate, LocalDateTime.now())
        );
        // 更新course_base表的发布状态未下线
        boolean is = courseBaseService.lambdaUpdate().eq(CourseBase::getId, courseId)
                .set(CourseBase::getStatus, "203003")
                .update();
        // 删除redis的de
        return row1 > 0 && is;
    }


}
